package model.db.table;

import java.sql.Date;
import java.sql.Time;
import model.db.Execute;
import model.db.Insert;
import model.db.Select;

/**
 * Author: Raoux Etienne
 * Student login: jc218934
 * Student number: 12448190
 * email: etienne.raoux@my.jcu.edu.au
 * alt email: jc218934@gmail.com
 */
public class Bid extends Table
{
    private Integer id;
    private Integer bidHistoryId;
    private Date date;
    private String bidder;
    private String bidType;
    private Integer qty;
    private Double amt;
    private static boolean lock = false;

    private static int maxId = 0;

    public Bid(int bidHistoryId, Date date, String bidder, String bidType, Integer qty, Double amt)
    {
        this.bidHistoryId = bidHistoryId;
        this.date = date;
        this.bidder = bidder;
        this.bidType = bidType;
        this.qty = qty;
        this.amt = amt;

    }

    public Bid() {
    }

    public Bid(Integer id, Integer bidHistoryId, Date date, String bidder, String bidType, Integer qty, Double amt)
    {
        this.id = id;
        this.bidHistoryId = bidHistoryId;
        this.date = date;
        this.bidder = bidder;
        this.bidType = bidType;
        this.qty = qty;
        this.amt = amt;
    }

     @Override
    public void insertInDb()
    {
        //id = getNextValidId();
        Insert.rowWithAutoincrementId(this);
    }

    public static int getNextValidId()
    {
        while (lock == true) {}
        lock = true;
        int val = Select.getInt("SELECT MAX (BID_ID) FROM BID")+1;
        maxId = maxId > val ? ++maxId : val;
        lock = false;
        return maxId;
    }

    public static void createTable()
    {
        System.out.println("creating Bid table");
        String stringToExecute = "CREATE TABLE BID ("
                + "BID_ID INTEGER NOT NULL PRIMARY KEY"
                + ", ITEM_ID INTEGER"
                + ", DATE DATETIME"
                + ", BIDDER STRING"
                + ", BID_TYPE STRING"
                + ", QTY INTEGER"
                + ", AMT DOUBLE)";
        Execute.execute(stringToExecute);
    }




    @Override
    public String toString()
    {
        return "Bid "+bidType +" bidder = "+bidder+" date "+date+" time "+new Time (date.getTime())+" qty "+qty+" Price "+amt+"\n";
    }

    @Override
    public String[] getTableNames() {
        return new String[]{"BID"};
    }

    @Override
    public String[] getPrimaryKeyNames()
    {
        return new String[]{"BID_ID"};

    }

    @Override
    public Object[] getPrimaryKeyValues() {
        return new Object[]
            {id};
    }

    @Override
    public Class[] getPrimaryKeyTypes() {
        return new Class[]
                {Integer.class};
    }

    @Override
    public String[] getColumnNames()
    {
        return new String[]
            {"BID_ID", "ITEM_ID", "DATE", "BIDDER", "BID_TYPE", "QTY", "AMT"};
    }

    @Override
    public Object[] getColumnValues()
    {
        return new Object[]
            {id, bidHistoryId, date, bidder, bidType, qty, amt};
    }

    @Override
    public Class[] getColumnTypes()
    {
        return new Class[]
                {Integer.class, Integer.class, Date.class, String.class, String.class, Integer.class, Double.class};
    }

    @Override
    public void initValues(Object[] values)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

 //   @Override
//    public void initValues(Object[] values)
//    {
//        Class[] columnTypes = getColumnTypes();
//        if (values.length == columnTypes.length)
//        {
//            id = (Integer) (values[0]);
//            table = (String) (values[1]);
//            persons = (Integer) (values[2]);
//            amt = (Double) (values[3]);
//            date = (Date) (values[4]);
//            closedBy = (Integer) (values[5]);
//            amtPaid = (Double) (values[6]);
//            ok = (Boolean) (values[7]);
//        }
//    }
}
